﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace School.WebPages.Download
{
    public partial class DownloadList : System.Web.UI.Page
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        protected string subMenu = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            int category = int.Parse(Request.QueryString["categories"]);
            
            var currentmenu = db.SubMenu.FirstOrDefault(s => s.Categories == category);
            subMenu = currentmenu.Name;

            if (!Page.IsPostBack)
            {
                AspNetPager1.RecordCount = db.DownloadFile.Where(f => f.Cagetory == category).Count();
                BindData();
            }
        }
        
        private void BindData()
        {
            int category = int.Parse(Request.QueryString["categories"]);
            SqlDataReader data = DAL.PagerHelper.GetPagerData("DownloadFile", "FID,Title,FIilePath,FileSize,ExtendName,Remark,UploadTime,AttachmentTitle,AttachmentFilePath,Hits", 
                "UploadTime", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "Cagetory=" + category);
            rep_DownloadFileItem.DataSource = data;
            rep_DownloadFileItem.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected string FileSizeConvert(long size)
        {
            string result = "0 Bytes";
            if (size < 1)
            {
                result = string.Format("{0} Bytes", Math.Round((double)size, 2));
            }
            else if (size >= 1 && size < 1024)
            {
                result = string.Format("{0} Bytes", Math.Round((double)size, 2));
            }
            else if (size >= 1024 && size < 1024 * 1024)
            {
                result = string.Format("{0} KB", Math.Round((double)size / 1024, 2));
            }
            else if (size >= 1024 * 1024 && size < 1024 * 1024 * 1024)
            {
                result = string.Format("{0} MB", Math.Round((double)size / (1024 * 1024), 2));
            }
            else if (size >= 1024 * 1024 * 1024 && size < (long)1024 * 1024 * 1024 * 1024)
            {
                result = string.Format("{0} GB", Math.Round((double)size / (1024 * 1024 * 1024), 2));
            }
            else
            {
                result = string.Format("{0} TB", Math.Round((double)size / ((double)1024 * 1024 * 1024 * 1024), 2));
            }

            return result;
        }

        protected void rep_DownloadFileItem_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                string[] args = e.CommandArgument.ToString().Split(',');
                Response.Redirect("~/WebPages/Download/DownloadExecute.aspx?FID=" + args[0] + "&FilePath=" + args[1]);
            }
        }
    }
}
